package cn.cyb.server.controller;

import cn.cyb.server.domain.AjaxResult;
import cn.cyb.server.domain.entity.SysUser;
import cn.cyb.server.domain.model.LoginBody;
import cn.cyb.server.security.SecurityUtils;
import cn.cyb.server.service.SysLoginService;
import cn.cyb.server.service.SysPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author yuebo
 * @date 2024/12/25
 */
@Tag(name = "SysLoginController", description = "登录验证")
@RestController
public class SysLoginController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private SysPermissionService permissionService;

    @Operation(summary = "用户登录", description = "")
    @RequestMapping("/user/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        logger.info("login loginBody=[{}]", loginBody);
        String token = sysLoginService.login(loginBody.getUsername(), loginBody.getPassword());
        Map dataMap = new HashMap();
        dataMap.put("token", token);
        return AjaxResult.success(dataMap);
    }

    @Operation(summary = "获取用户信息", description = "")
    @GetMapping("/user/info")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        Set<String> permission = permissionService.getRolePermission(user);
        user.setRoles(permission);
        return AjaxResult.success(user);
    }

    @Operation(summary = "退出登录", description = "")
    @RequestMapping("/user/logout")
    public AjaxResult logout() {
        return AjaxResult.success();
    }
}
